package com.skyline.spot.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;
import com.skyline.spot.dao.SpotCharacteristicDao;
import com.skyline.spot.mapper.SpotCharacteristicMapper;
import com.skyline.spot.model.SpotCharacteristic;

@Repository("spotCharacteristicDao")
public class SpotCharacteristicDaoImpl extends BaseDaoImpl implements
		SpotCharacteristicDao {

	/**
	 * insert into
	 * spotcharacteristic(spotId,spotName,spotPortrait,ownerId,ownerNickname
	 * ,ownerPortrait
	 * ,creatorId,creatorNickname,creatorPortrait,characteristicTitle,
	 * characteristicDetail,albumId,createtime,updatetime)
	 * values(?,?,?,?,?,?,?,?,?,?,?,current_timestamp,current_timestamp)
	 */
	@Value("${SpotCharacteristicDao.insertCharacteristic}")
	private String insertCharacteristicSQL;
	/**
	 * update spotCharacteristic set characteristicTitle=? ,
	 * characteristicDetail =? updatetime = current_timestamp where id = ?
	 */
	@Value("${SpotCharacteristicDao.updateCharacteristic}")
	private String updateCharacteristicSQL;
	/**
	 * update spotcharacteristic set star =? ,votes = votes + 1 where id=?
	 */
	@Value("${SpotCharacteristicDao.updateStar}")
	private String updateStarSQL;
	/**
	 * update spotcharacteristic set activity = ? where id=?
	 */
	@Value("${SpotCharacteristicDao.updateActivity}")
	private String updateactivitySQL;
	@Value("${SpotCharacteristicDao.updatePortrait}")
	private String updatePortraitSQL;
	/**
	 * select * from spotCharacteristic where spotid = ? and activity= ?
	 */
	@Value("${SpotCharacteristicDao.queryCharacteristicBySpotId}")
	private String queryCharacteristicBySpotIdSQL;

	/**
	 * select * from spotCharacteristic where id = ? and activity= ?
	 */
	@Value("${SpotCharacteristicDao.queryCharacteristicById}")
	private String queryCharacteristicByIdSQL;

	@Override
	public Long insertCharacteristic(Long spotId, String spotName,
			String spotPortrait, Long ownerId, String ownerNickname,
			String ownerPortrait, Long creatorId, String creatorNickName,
			String creatorPortrait, String charateristicTile,
			String charateristicDetail, Integer consume, Long albumId) {
		return this.insertWithIdReturn(insertCharacteristicSQL, spotId,
				spotName, spotPortrait, ownerId, ownerNickname, ownerPortrait,
				creatorId, creatorNickName, creatorPortrait, charateristicTile,
				charateristicDetail, consume, albumId);
	}

	@Override
	public void updateCharacteristic(Long id, String charateristicTile,
			String charateristicDetail) {
		jdbcTemplate.update(updateCharacteristicSQL, charateristicTile,
				charateristicDetail, id);

	}

	@Override
	public void updateCharacteristic(Long id, String portrait) {
		jdbcTemplate.update(updatePortraitSQL, portrait, id);

	}

	@Override
	public void updateCharacteristic(Long id, Activity activity) {
		jdbcTemplate.update(updateactivitySQL, activity, id);

	}

	@Override
	public void updateSpotCharaStar(Long scId, Float star) {
		
		jdbcTemplate.update(updateStarSQL, star, scId);

	}

	@Override
	public List<SpotCharacteristic> queryCharacteristicBySpotrId(Long spotId,
			Page page, Activity activity) {
		// return jdbcTemplate.query(queryCharacteristicByOwnerIdSQL,
		// spotCharacteristicMapper.getMapper(),Activity.NORMAL.toString());
		return this.getPaginationResult(queryCharacteristicBySpotIdSQL, page,
				SpotCharacteristicMapper.getMapper(), spotId,
				activity.toString());
	}

	@Override
	public SpotCharacteristic queryCharacteristicById(Long id, Activity activiy) {
		List<SpotCharacteristic> list = jdbcTemplate.query(
				queryCharacteristicByIdSQL,
				SpotCharacteristicMapper.getMapper(), id, activiy.toString());
		if (list == null)
			return null;
		else
			return list.get(0);
	}

}
